package org.ekstrabilet.stadium;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.ekstrabilet.game.beans.Game;
import org.ekstrabilet.stadium.beans.Row;
import org.ekstrabilet.stadium.beans.Seat;
import org.ekstrabilet.stadium.beans.Sector;
import org.ekstrabilet.stadium.beans.Stadium;
import org.ekstrabilet.stadium.constants.StadiumConstants;
import org.ekstrabilet.ticket.CartPane;
import org.ekstrabilet.ticket.beans.Ticket;
/**
 * 
 * @author Maciej Koch
 * Renders selected preview. If it is in selling mode adds ticket to shopping cart.
 */
public class SectorsView extends JPanel implements MouseListener, MouseMotionListener {
	
	Sector sector;
	int height = StadiumConstants.viewHeight;
	int width = StadiumConstants.viewWidth;
	
	//starting position of the sector
	int startX = 0;
	int startY = 0;
	//moving mouse
	int mx = 0;
	int my = 0;
	//starting position of the mouse
	int smx = 0;
	int smy = 0;
	
	private boolean sale;
	private CartPane cart;
	private Game game;
	private Stadium stadium;
	private int tribune;
	
	float scale = StadiumConstants.sectorViewScale;
	
	/**
	 * 
	 * @param sale
	 * true if selling mode, false if not
	 */
	public SectorsView(boolean sale, CartPane cart) {
		this.sale = sale;
		this.cart = cart;
		
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	public void setStadium(Stadium s){
		stadium = s;
	}
	public void setGame(Game g) {
		game = g;
	}
	public void paintComponent(Graphics g) {
		
		//background
		g.setColor(Color.black);
		g.fillRect(0, 0, width, height);
		
//		g.setColor(Color.white);
//		g.fillRect((int)(sector.x * scale) + startX + mx, (int)(sector.y * scale) + my + startY, (int)(sector.width * scale), (int)(sector.height * scale));

		
		//Image seatImg = Toolkit.getDefaultToolkit().getImage("data/Seat-taken.png");

		for(Row r : sector.getRows())
			for(Seat s : r.getSeats()){
				if(s.isEmpty()) g.setColor(Color.green);
				else g.setColor(Color.red);				
				g.fillRect((int)(s.x * scale) + startX + mx, (int)(s.y * scale) + my + startY, (int)(s.width * scale), (int)(s.height * scale));
				
				g.setColor(Color.black);
				g.drawRect((int)(s.x * scale) + startX + mx, (int)(s.y * scale) + my + startY, (int)(s.width * scale), (int)(s.height * scale));

			}
				//g.drawImage(seatImg, (int)(s.x * scale) + startX + mx, (int)(s.y * scale) + my + startY, null);
	}
	/**
	 * Prepares seat, rows in each of sectors
	 * @param sector to render
	 * @param tribune - id of tribune in which a sector is.
	 */
	public void setSector(Sector sector, int tribune){
		/*
		 * tribune:
		 * 0, 1 - horizontal
		 * 2, 3 - vertical
		 */
		this.tribune = tribune;
		this.sector = sector;	
		//sector
		int sw = (int)(sector.width * scale);
		int sh = (int)(sector.height * scale);
		if(sw > width) startX = 10;
		else startX = (width - sw)/2;
		if(sh > height) startY = 10;
		else startY = (height - sh)/2;
		
		for(int i = 0 ; i < sector.getRows().size() ; i++){
			//horizontal
			if(tribune < 2)
				sector.getRow(i).set(0, i * StadiumConstants.seatS, sector.width, StadiumConstants.seatS);
			//vertical
			else
				sector.getRow(i).set(i * StadiumConstants.seatS, 0, StadiumConstants.seatS, sector.height);
			
			Row r = sector.getRow(i);
			for(int j = 0 ; j < sector.getRow(i).getSeats().size() ; j++)
				//horizontal
				if(tribune < 2)
					sector.getRow(i).getSeat(j).set(r.x + j * StadiumConstants.seatS, r.y, StadiumConstants.seatS, StadiumConstants.seatS);
				else
					sector.getRow(i).getSeat(j).set(r.x, r.y + j * StadiumConstants.seatS, StadiumConstants.seatS, StadiumConstants.seatS);

		}
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		
		//left button
		if((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0){
			int x = e.getX() - smx;
			int y = e.getY() - smy;
			
			setMouseMoved(x, y);
			repaint();
		}
		
	}
	@Override
	public void mouseMoved(MouseEvent e) {

	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		//left mouse button
		if(e.getButton() == MouseEvent.BUTTON1) {
			smx = e.getX() - mx;
			smy = e.getY() - my;
			return;
		}
		
		start : if(sale) {
			float sx = sector.x + ( -mx - startX + e.getX())/scale;
			float sy = sector.y + ( -my - startY + e.getY())/scale;
		
			if(sector.isOn(sx,sy)) {
				sx = ( -mx - startX + e.getX())/scale;
				sy = ( -my - startY + e.getY())/scale;
				for(Row r : sector.getRows())
					if(r.isOn(sx, sy))
						for(Seat s : r.getSeats())
							if(s.isOn(sx, sy))
								if(s.isEmpty()) {
									s.setEmpty(false);
									Ticket t = new Ticket();
									t.setStadium(stadium);
									t.setSectorId(sector.getSectorId()); 
									t.setGameId(game.getGameID());
									t.setRow(sector.getRows().indexOf(r));
									t.setSeat(r.getSeats().indexOf(s));
									t.setTribune(tribune);
									cart.addTicket(t);
									break start;
								}
			}
		}
		repaint();
	}
	@Override
	public void mouseReleased(MouseEvent e) {

	}
	/**
	 * Restarts mouse positions
	 */
	public void restart() {
		mx = 0; 
		my = 0;
	}
	public void setScale(int s) {
		scale = s;
		repaint();
	}
	private void setMouseMoved(int mx, int my) {
		this.mx = mx;
		this.my = my;
	}
	
}
